//
//  JQMineVC.m
//  JQLive
//
//  Created by Bruce on 2020/8/7.
//  Copyright © 2020 mlkq. All rights reserved.
//

#import "JQMineVC.h"
#import "UIView+Extension.h"
#import "JQMineCell.h"
#import "JQMineProfileVC.h"
#import "JQUserLoginVC.h"
#import "JQMineSetVC.h"

@interface JQMineVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIImageView *topImageView;//头像
@property (nonatomic, strong) UILabel *topLb;// 昵称
@property (nonatomic, strong) UIButton *loginBtn;// 未登录 按钮
@property (nonatomic, strong) UILabel *nameDetailLb;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSArray *imageArr;
@property (nonatomic, copy) NSArray *textArr;
@end

@implementation JQMineVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImageView *topImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 215)];
    [self.view addSubview:topImageV];
    topImageV.image = [UIImage svgImageNamed:@"mine_background" size:topImageV.bounds.size];
    
    self.titleView.title = nil;
    self.imageArr = @[@[@"mine_liveAnchorIcon"], @[@"mine_share"], @[@"mine_feedback", @"mine_setIcon"]];
    self.textArr = @[@[@"申请主播"], @[@"分享给好友"], @[@"意见反馈", @"更多设置"]];
    [self addTopView];
    [self addTableView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)addTopView {
    MY_WEAK;
    
    // 增加手势
    UIView *tapGes = [[UIView alloc] initWithFrame:CGRectMake(8, 62, SCREEN_WIDTH, 80)];
    [self.view addSubview:tapGes];
//    tapGes.backgroundColor = [UIColor clearColor];
    [tapGes my_addTapGes:^{
        if (![self.navigationController.topViewController isKindOfClass:[JQMineProfileVC class]]){
            JQMineProfileVC *vc = [[JQMineProfileVC alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }];
    
    UIImageView *topImageView = [[UIImageView alloc] init];
    [self.view addSubview:topImageView];
    self.topImageView = topImageView;
    topImageView.backgroundColor = [UIColor whiteColor];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        MY_STRONG;
        make.left.mas_equalTo(strongSelf.view.mas_left).offset(8);
        make.top.mas_equalTo(strongSelf.view.mas_top).offset(62);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    topImageView.image = [UIImage svgImageNamed:@"mine_headIcon" size:CGSizeMake(80, 80)];
//    [topImageView.image createImageWithSize:CGSizeMake(80, 80) gradientColors:@[kColorWithHex(0xFFE9BE), kColorWithHex(0xED9C4E)] percentage:@[@0,@1] gradientType:GradientFromTopToBottom];
    
    //    [self.view layoutSubviews];// 有效刷新 mansonry  布局
    //    [self.view layoutIfNeeded];// 有效刷新 mansonry  布局
    //    [self.view setNeedsLayout];//  无效刷新 mansonry  布局
    
    [topImageView layoutIfNeeded];
    [topImageView round:40 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight];
    
    UIView *circle = [[UIView alloc] init];
    //    circle.backgroundColor = [UIColor lightGrayColor];
    [topImageView addSubview:circle];
    [circle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(topImageView);
    }];
    
    CGFloat width = 40;
    CGFloat height = 40;
    //创建背景圆环
    CAShapeLayer *trackLayer = [CAShapeLayer layer];
    trackLayer.frame = circle.bounds;
    //清空填充色
    trackLayer.fillColor = [UIColor clearColor].CGColor;
    //设置画笔颜色 即圆环背景色
    //    trackLayer.strokeColor =  kColorWithHex(0xFFE9BE).CGColor;// [UIColor colorWithRed:170/255.0 green:210/255.0 blue:254/255.0 alpha:1].CGColor;
    trackLayer.lineWidth = 4;
    //设置画笔路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(width, height) radius:width startAngle:- M_PI_2 endAngle:-M_PI_2 + M_PI * 2 clockwise:YES];
    //path 决定layer将被渲染成何种形状
    trackLayer.path = path.CGPath;
    [circle.layer addSublayer:trackLayer];
    
    CALayer *gradientLayer = [CALayer layer];
    gradientLayer.frame = CGRectMake(0, 0, 80, 80);//circle.bounds;
    
    CAGradientLayer *gradientLayer1 = [CAGradientLayer layer];
    gradientLayer1.frame = CGRectMake(0, 0, 80, 80);//circle.bounds;//CGRectMake(width/2.0, 0, width/2.0,  height/2.0);
    gradientLayer1.colors = @[(__bridge id)kColorWithHex(0xFFE9BE).CGColor, (__bridge id)kColorWithHex(0xED9C4E).CGColor];
    //startPoint和endPoint属性，他们决定了渐变的方向。这两个参数是以单位坐标系进行的定义，所以左上角坐标是{0, 0}，右下角坐标是{1, 1}
    //startPoint和pointEnd 分别指定颜色变换的起始位置和结束位置.
    //当开始和结束的点的x值相同时, 颜色渐变的方向为纵向变化
    //当开始和结束的点的y值相同时, 颜色渐变的方向为横向变化
    //其余的 颜色沿着对角线方向变化
    gradientLayer1.startPoint = CGPointMake(0, 0);//CGPointMake(0.2, 0);
    gradientLayer1.endPoint = CGPointMake(1, 1);//CGPointMake(0.8, 1);
    [gradientLayer addSublayer:gradientLayer1];
    
    [circle.layer addSublayer:gradientLayer];
    
    CAShapeLayer *progressLayer = [CAShapeLayer layer];
    progressLayer.frame = circle.bounds;
    progressLayer.fillColor = [UIColor clearColor].CGColor;
    progressLayer.strokeColor = [UIColor colorWithRed:170/255.0 green:210/255.0 blue:254/255.0 alpha:1].CGColor;
    progressLayer.lineWidth = 4;
    progressLayer.path = path.CGPath;
    gradientLayer.mask = progressLayer;
    
    [circle layoutIfNeeded];
    
    [circle round:40 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight];
   
    // topLb 用于 昵称
    UILabel *topLb = [[UILabel alloc] init];
    [self.view addSubview:topLb];
    self.topLb = topLb;
    [topLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(topImageView.mas_right)
        .offset(10);
        make.top.mas_equalTo(topImageView.mas_top).offset(6);
        make.size.mas_equalTo(CGSizeMake(200, 25));
    }];
    topLb.textColor = [UIColor whiteColor];
    topLb.font = [UIFont systemFontOfSize:18];
    topLb.text = @"欢迎使用九球直播";
    
    UIImageView *rightArrowImageV = [[UIImageView alloc] initWithImage:[UIImage svgImageNamed:@"mine_rightArrow_white" size:CGSizeMake(7, 10)]];
    [self.view addSubview:rightArrowImageV];
    
    [rightArrowImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        MY_STRONG;
        make.right.mas_equalTo(strongSelf.view.mas_right).offset(-52);
        make.centerY.equalTo(topLb);
        make.size.mas_equalTo(CGSizeMake(7, 10));
    }];
    
    
    UIButton *loginBtn = [[UIButton alloc] init];
    [self.view addSubview:loginBtn];
    self.loginBtn = loginBtn;
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(topImageView.mas_right).offset(10);
        make.bottom.mas_equalTo(topImageView.mas_bottom).offset(-8);
        make.size.mas_equalTo(CGSizeMake(88, 32));
    }];
    
    [loginBtn setTitle:@"立即登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:kColorWithHex(0x935400) forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [loginBtn layoutIfNeeded];
    
    // gradient
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = loginBtn.bounds;
    gl.startPoint = CGPointMake(0.98, 0.5);
    gl.endPoint = CGPointMake(0, 0.51);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:255/255.0 green:154/255.0 blue:48/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:254/255.0 green:229/255.0 blue:135/255.0 alpha:1.0].CGColor];
    gl.locations = @[@(0), @(1.0f)];
//    view.layer.cornerRadius = 18;
//    CALayer *loginLayer = [CALayer layer];
//    loginLayer.frame = loginBtn.bounds;
//    [loginLayer addSublayer:gl];
    [loginBtn.layer addSublayer:gl];
    [loginBtn.layer insertSublayer:gl atIndex:0];
    [loginBtn round:16 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight];
    [loginBtn addTapBlock:^(UIButton * _Nonnull btn) {
        // 跳转登录页面
        [self.navigationController pushViewController:[[JQUserLoginVC alloc] init] animated:YES];
//        [self alterSexPortrait];
    }];
    
    
    UILabel *nameDetailLb = [[UILabel alloc] init];
    [self.view addSubview:nameDetailLb];
    self.nameDetailLb = nameDetailLb;
    [nameDetailLb mas_makeConstraints:^(MASConstraintMaker *make) {
        MY_STRONG;
        make.top.mas_equalTo(topLb.mas_bottom).offset(8);
        make.left.mas_equalTo(topImageView.mas_right).offset(10);
        make.right.mas_equalTo(strongSelf.view.mas_right).offset(-39);
        make.height.equalTo(@(17));
    }];
    nameDetailLb.text = @"国内知名足彩竞彩专家，对德甲法甲临…";
    nameDetailLb.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
//    nameDetailLb.textColor = [UIColor whiteColor];
//    nameDetailLb.alpha = 0.7;
    nameDetailLb.font = [UIFont systemFontOfSize:12];
    nameDetailLb.hidden = YES;
    
    // 全局判断是否 登录
    
//    loginBtn.hidden = YES;
    
}
// tabview 加载
- (void)addTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 172, SCREEN_WIDTH, SCREEN_HEIGHT-172-MY_BottomTabbarHeight) style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    tableView.backgroundColor = kColorWithHex(0xF2F4FC);
    [tableView round:20 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[JQMineCell class] forCellReuseIdentifier:NSStringFromClass([JQMineCell class])];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.scrollEnabled = NO;
    
    // 测试代码
//            UITextField *textFL = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 80, 40)];
//            textFL.backgroundColor = [UIColor redColor];
//    [self.tableView addSubview:textFL];
//
//    UITextField *textFL1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 550, 280, 60)];
//            textFL1.backgroundColor = [UIColor redColor];
//    [self.view addSubview:textFL1];
    
}
// 可以自定义 弹框 view
#pragma mark----- 选择性别弹出框
-(void)alterSexPortrait{
    /*  弹出提示框
     */
    //初始化提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择你的性别" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    //按钮：男
    [alert addAction:[UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 点击 男
        NSLog(@"action：%@", action);
        
    }]];
    //按钮：女
    [alert addAction:[UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        // 女
        NSLog(@"action：%@", action);
        
        
    }]];
    //按钮：保密
    [alert addAction:[UIAlertAction actionWithTitle:@"保密" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        // 保密
        NSLog(@"action：%@", action);
        
        
    }]];
    //按钮：取消，类型：UIAlertActionStyleCancel
//    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - InitNAV
///// 设置导航栏的背景图，默认为 NavBarBackgroundImage
- (nullable UIImage *)navigationBarBackgroundImage{
    //    return [UIImage new];
    return [UIImage svgImageNamed:@"mine_background" size:CGSizeMake(SCREEN_WIDTH, 215)];
#pragma mark - 设置 导航栏的背景图  效果
    //     return [UIImage imageNamed:@"home_background"]; //这里的timg就是那张叶子图
}

/// 设置当前导航栏的 barTintColor，默认为 NavBarBarTintColor
- (nullable UIColor *)navigationBarBarTintColor{
    return [UIColor clearColor];
#pragma mark - 设置 导航栏的背景 颜色
    //    return MYHome_backgroundColor;
}
// 系统状态栏
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
#pragma mark - deleagte
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return 1;
    }
    return 2;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headV = [[UIView alloc] init];
    headV.backgroundColor = tableView.backgroundColor;
    return headV;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footV = [[UIView alloc] init];
    footV.backgroundColor = tableView.backgroundColor;
    return footV;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 48;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    JQMineCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JQMineCell class]) forIndexPath:indexPath];
    cell.backgroundColor = tableView.backgroundColor;
    if (indexPath.row == 0 && ([tableView numberOfRowsInSection:indexPath.section]-1 == 0)){
        [cell layoutIfNeeded];
        [cell.cellBackgroundV round:10 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight];
    }else if (indexPath.row == 0) {
        [cell layoutIfNeeded];
        [cell.cellBackgroundV round:10 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    }else if ([tableView numberOfRowsInSection:indexPath.section]-1 == indexPath.row) {
        [cell layoutIfNeeded];
        [cell.cellBackgroundV round:10 RectCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
    }
    [cell setCellImage:[UIImage svgImageNamed:self.imageArr[indexPath.section][indexPath.row] size:CGSizeMake(20, 20)] cellLb:self.textArr[indexPath.section][indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2 && indexPath.row == 1) {
        [self.navigationController pushViewController:[[JQMineSetVC alloc] init] animated:YES];
        return;
    }
}
@end
